﻿using Newtonsoft.Json;
using Snap7;

namespace HamcoWcs.Models.Devices
{
    public class RasterDto
    {
        public string Name { get; set; }
        public bool ErrorFlag { get; set; }
        public bool BypassFlag { get; set; }
        public string MachinePosition { get; set; }
        public string ErrorOffset { get; set; }
        public string BypassReadOffset { get; set; }
        public string BypassWriteOffset { get; set; }
        public string Remark { get; set; }

        private int errorOffset = -1;
        private int errorPosition = -1;
        private int bypassReadOffset = -1;
        private int bypassReadPosition = -1;
        private int bypassWriteOffset = -1;
        private int bypassWritePosition = -1;


        public void CalcOffset()
        {
            var errorOffsets = ErrorOffset.Split('.');
            errorOffset = int.Parse(errorOffsets[0]);
            errorPosition = int.Parse(errorOffsets[1]);
            var bypassReadOffsets = BypassReadOffset.Split('.');
            bypassReadOffset = int.Parse(bypassReadOffsets[0]);
            bypassReadPosition = int.Parse(bypassReadOffsets[1]);
            var bypassWriteOffsets = BypassWriteOffset.Split('.');
            bypassWriteOffset = int.Parse(bypassWriteOffsets[0]);
            bypassWritePosition = int.Parse(bypassWriteOffsets[1]);
        }

        /// <summary>
        /// 更新WCS接收状态
        /// </summary>
        /// <param name="buffer">DB103</param>
        public void UpdateRecv(byte[] buffer)
        {
            if (errorOffset < 0) { CalcOffset(); }
            if (buffer == null || buffer.Length == 0 || buffer.Length <= errorOffset) return;
            ErrorFlag = buffer.ReadBool(errorOffset, errorPosition);
            BypassFlag = buffer.ReadBool(bypassReadOffset, bypassReadPosition);
        }


        /// <summary>
        /// 设置该光栅Bypass
        /// </summary>
        /// <param name="s7Client">S7客户端</param>
        /// <returns></returns>
        public bool SetBypass(S7Client s7Client)
        {
            byte[] buffer = new byte[1];
            var res = s7Client.DBRead(104, bypassWriteOffset, 1, buffer);
            if (res != 0) return false;
            byte msg = buffer[0];
            msg = msg.SetBit(bypassWritePosition, true);
            return s7Client.DBWrite(104, bypassWriteOffset, 1, new byte[] { msg }) == 0;
        }

        /// <summary>
        /// 恢复该光栅Bypass
        /// </summary>
        /// <param name="s7Client">S7客户端</param>
        /// <returns></returns>
        public bool ResetBypass(S7Client s7Client)
        {
            byte[] buffer = new byte[1];
            var res = s7Client.DBRead(104, bypassWriteOffset, 1, buffer);
            if (res != 0) return false;
            byte msg = buffer[0];
            msg = msg.SetBit(bypassWritePosition, false);
            return s7Client.DBWrite(104, bypassWriteOffset, 1, new byte[] { msg }) == 0;
        }


    }

    public class RasterSendInfo
    {
        public RasterSendInfo()
        {

        }

        public RasterSendInfo(RasterDto raster, bool setRasterStatus, string userName)
        {
            Raster = raster;
            SetRasterStatus = setRasterStatus;
            UserName = userName;
        }

        public RasterDto Raster { get; set; }
        /// <summary>
        /// false:屏蔽，true:解除屏蔽
        /// </summary>
        public bool SetRasterStatus { get; set; }
        public string UserName { get; set; }
    }

    public class RasterBypassModel
    {
        public string Name { get; set; }
        public string Remark { get; set; }
    }

    public class UIRaster
    {
        public UIRaster()
        {

        }
        public string Name { get; set; }
        public string MachinePosition { get; set; }
        public bool ErrorFlag { get; set; }
        public bool BypassFlag { get; set; }

        public string Remark { get; set; }

        public UIRaster(RasterDto raster)
        {
            Name = raster.Name;
            MachinePosition = raster.MachinePosition;
            Update(raster);
        }

        public void Update(RasterDto raster)
        {
            ErrorFlag = raster.ErrorFlag;
            BypassFlag = raster.BypassFlag;
            Remark = raster.Remark;
        }
        //public string MachinePosition { get; set; }

    }
}
